const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../config/api.js');

Page({
  data: {
    imgUrl: app.globalData.imgurl,
    array: ['手机', '座机', '传真'],
    array1: ['手机', '座机', '传真'],
		selectProjectList: [],
		projectList:[],
    index: 0,
    index1: 0,
		mobile:'',
		phone:'',
    tel2:false,
    addTel:true,
    loulistShow:false,
  },
	onShow: function () {
		this.setData({
			index: 0,
			index1: 0,
			name: '',
			mobile: '',
			phone: '',
			loulistShow: false,
			selectProjectList: []
		});
		this.getProjectList();
	},
	getProjectList: function () {
		let that = this;
		common.request(api.ProjectList, {}, 'post').then(function (data) {
			if (data.type === 'success') {
				that.setData({
					projectList: data.data.projectList
				});
			}
		});
	},
	tuijian: function () {
		if (!wx.getStorageSync("memberId")) {
			wx.navigateTo({
				url: '/pages/shouquan/shouquan'
			});
		} else {
			let that = this;
			let name = that.data.name;
			let mobile = that.data.mobile;
			let tel2 = that.data.tel2;
			let phone = that.data.phone;
			let array = that.data.array;
			let array1 = that.data.array1;
			let index = that.data.index;
			let index1 = that.data.index1;
			let selectProjectList = that.data.selectProjectList;
			if (name == null || name == '') {
				common.showErrorToast('请输入姓名');
				return false;
			}
			if (mobile == null || mobile == '') {
				common.showErrorToast('请输入联系方式');
				return false;
			}
			if (tel2 == true && (phone == null || phone == '')) {
				common.showErrorToast('请输入联系方式');
				return false;
			}
			if (selectProjectList.length < 1) {
				common.showErrorToast('选择意向楼盘');
				return false;
			}
			let projectIds = selectProjectList.map(function (element, index, array) {
				return element.id;
			});
			let projectName = selectProjectList.map(function (element, index, array) {
				return '[' + element.areaName + ']' + element.name;
			});
			let data = {
				name: name,
				mobile: array[index] + '：' + mobile,
				phone: array1[index1] + '：' + phone,
				projectIds: projectIds,
				projectName: projectName,
				tel2: tel2
			}
			common.request(api.ProjectCustomerAdd, data, 'post').then(function (data) {
				if (data.type == 'success') {
					common.showSuccessToast(data.content);
					setTimeout(function () {
						wx.navigateTo({
							url: '/pages/ucenter/tuijian/tuijian'
						});
					}, 800)
				} else {
					common.showErrorToast(data.content);
				}
			});
		}
	},
	addTel: function () {
		this.setData({
			tel2: true,
			addTel: false
		})
	},
	nameInput: function (e) {
		var name = e.detail.value;
		this.setData({
			name: name
		});
	},
	mobileInput: function (e) {
		var mobile = e.detail.value;
		this.setData({
			mobile: mobile
		});
	},
	phoneInput: function (e) {
		var phone = e.detail.value;
		this.setData({
			phone: phone
		});
	},
	bindPickerChange: function (e) {
		this.setData({
			index: e.detail.value
		})
	},
	bindPickerChange1: function (e) {
		this.setData({
			index1: e.detail.value
		})
	},
  louShow: function () {
    this.setData({
      loulistShow: true
    })
  },
  closePop: function () {
    this.setData({
      loulistShow: false
    })
	},
	selectLou: function (e) {
		var index = e.currentTarget.dataset.index;
		let projectList = this.data.projectList;
		let selectProjectList = this.data.selectProjectList;
		let projectId = projectList[index].id;
		let isPush = 1;
		for (var i = 0; i < selectProjectList.length; i ++) {
			if (projectId == selectProjectList[i].id) {
				isPush = 0;	
			}
		}
		if (isPush == 1) {
			selectProjectList.push(projectList[index]);
		}
		this.setData({
			selectProjectList: selectProjectList,
			loulistShow: false
		});
	},
  delLou: function (e) {
    var index = e.currentTarget.dataset.index;
		let selectProjectList = this.data.selectProjectList;
		selectProjectList.splice(index, 1);
		this.setData({
			selectProjectList: selectProjectList
		});
  }
})